<# # It is recommended to test the script on a local machine for its purpose and effects. # ManageEngine Endpoint Central will not be responsible for any # damage/loss to the data/setup based on the behavior of the script. # Description : To fetch the firewall results in a common share path # Configuration Type - COMPUTER # Limitation: The common shared path must be accessible by everyone. # Arguments: The common shared path must be hardcoded inside the script. #> # Get the hostname of the local server $hostname = $env:COMPUTERNAME # Define the shared folder path and the folder name $sharedFolderPath = "\\CommonSharePathName" # Replace this with your shared network path $folderName = "EndpointCentral" $folderPath = Join-Path -Path $sharedFolderPath -ChildPath $folderName $csvPath = Join-Path -Path $folderPath -ChildPath "$hostname.csv" # Initialize an array to store the results $results = @() # Error handling block to catch any issues during firewall rule collection try { # Retrieve the list of firewall rules where the action is "Block" $firewallRules = Get-NetFirewallRule | Where-Object { $_.Action -eq "Block" } # Loop through each firewall rule and collect additional details foreach ($rule in $firewallRules) { # Get associated address filter details for the current rule $ruleDetails = Get-NetFirewallAddressFilter -AssociatedNetFirewallRule $rule # Store the relevant details in the results array $results += [PsCustomObject]@{ Hostname = $hostname RuleName = $rule.Name DisplayName = $rule.DisplayName Description = $rule.Description Direction = $rule.Direction Enabled = $rule.Enabled RemoteAddress = $ruleDetails.RemoteAddress LocalPort = $ruleDetails.LocalPort RemotePort = $ruleDetails.RemotePort } } } catch { # Handle errors and display a message Write-Error "An error occurred while collecting firewall rules or processing the data: $_" } # Error handling block for creating the folder and exporting the CSV try { # Check if the folder exists, if not, create it if (-not (Test-Path $folderPath)) { New-Item -Path $folderPath -ItemType Directory Write-Host "Folder '$folderPath' created successfully." } # Check if the CSV file already exists if (Test-Path $csvPath) { # Append the results to the existing CSV file $results | Export-Csv -Path $csvPath -Append -NoTypeInformation } else { # Create a new CSV file and export the results $results | Export-Csv -Path $csvPath -NoTypeInformation } } catch { # Handle errors and display a message Write-Error "An error occurred while creating the folder or exporting data to the CSV file: $_" }